//
//  ViewController.m
//  UI10_UITableView
//
//  Created by Scott on 16/3/22.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "TableViewOfContact.h"


@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

// 联系人数组
@property (nonatomic, retain) NSArray *arrOfContent;

@end

@implementation ViewController

- (void)dealloc {
    
    [_arrOfContent release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"TableView";
    
    [self handleData];
    
    [self createTableView];
}

#pragma mark - Data 
- (void)handleData {
    
    self.arrOfContent = @[@{@"name":@"Tom",
                            @"image":@"1.jpg",
                            @"phone":@"12334"},
                          
                          @{@"name":@"zhang",
                            @"image":@"2.jpg",
                            @"phone":@"342"},
                          
                          @{@"name":@"lisi",
                            @"image":@"3.jpg",
                            @"phone":@"7878"
                            },
                          
                          @{@"name":@"wangwu",
                            @"image":@"4.jpg",
                            @"phone":@"9191"}];
    
    
    
}


#pragma mark - 知识点1: UITableView
- (void)createTableView {
    
    // 普通风格的tableView
    TableViewOfContact *tableView = [[TableViewOfContact alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];

    [self.view addSubview:tableView];
    [tableView release];
    
    // 指定代理人.
    tableView.dataSource = self;
    tableView.delegate = self;
    
}

#pragma mark - 知识点2: TableViewDataSource 相关协议方法
/** 必须实现协议方法: 1/2 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrOfContent.count;
}


/** 必须实现协议方法: 2/2. */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __func__);
    
    
    NSLog(@"%ld, %ld", indexPath.section, indexPath.row);
    
    
    // static 在静态区存储变量, 且只初始化一次. 内存进行了优化.
    static NSString *pool1 = @"pool";
    
    // 从重用池中取cell对象.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pool1];
    
    // 判断cell是否为nil
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:pool1];
    }
    
    // cell赋值.
    cell.textLabel.text = [[self.arrOfContent objectAtIndex:indexPath.row] objectForKey:@"name"];
    
    
    cell.imageView.image = [UIImage imageNamed:[[self.arrOfContent objectAtIndex:indexPath.row] objectForKey:@"image"]];
    
    
    cell.detailTextLabel.text = [[self.arrOfContent objectAtIndex:indexPath.row] objectForKey:@"phone"];
    
    
    return cell;
}



#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
